<template lang="html">
    <div class="classify">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>&nbsp;轮播图列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" round @click="urlAdd()"><i class="el-icon-plus"> </i>&nbsp;&nbsp;新增轮播图</el-button>
            <div style="height:20px;"></div>
            <el-select v-model="bannerValue" @change="changeValue" placeholder="请选择">
                <el-option
                v-for="item in bannerClass"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
            <el-table :data="bannerList" style="width: 100%">
                <el-table-column prop="id" label="ID" width="180"></el-table-column>
                <el-table-column label="轮播图" width="400" align="center">
                    <template slot-scope="scope" >
                        <div><img :src="scope.row.url" style="width:300px;height:100px"></div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope" >
                        <el-button class="del"  size="small" type="text"  @click="changeDelete(scope.row)" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {getBannerList,postBannerDel} from '@/api/banner/list'
    export default {
        name: "banner",
        data() {
            return {
                location:1,//位置：0不限、1PC首页、2APP首页
                bannerList:[],//轮播图列表
                bannerClass:[
                    {
                        value: '1',
                        label: 'PC轮播图'
                    },
                    {
                        value: '2',
                        label: 'APP轮播图'
                    },
                ],
                bannerValue:'PC轮播图',
                delVisible:false,//删除弹框
                Id:0,//轮播图id
            }
        },
        created(){
            //获取分类列表
            this.getBannerList();
        },
        mounted() {
            
        },
        methods: {
            //获取轮播图列表
            getBannerList(){
                getBannerList(this.location).then(res => {
                    // console.log(res);
                    this.bannerList = [];
                    this.bannerList = res.data;
                })
            },
            //点击切换PC，APP
            changeValue(value){
                this.location = value;
                //获取新列表
                this.getBannerList();
            },
            //跳转新增
            urlAdd(){
                this.$router.push({ path: '/banner/add'});
            },
            //删除轮播图
            changeDelete(ev){
                this.Id = ev.id;//保存轮播图id
                this.delVisible = true;//打开弹框
            },
            //确定，删除操作
            deleteRow(){
                postBannerDel(this.Id).then(res => {
                    if(res.code == 200){
                        this.$message({
                            message: '删除成功！',
                            type: 'success'
                        });
                        this.getBannerList();//重新加载列表
                    }else {
                        this.$message.error('删除失败！');
                    }
                })
                this.delVisible = false;//关闭弹框
            },
        }
    }
</script>

<style lang="css" scoped>
.del {
    color: #f00;
}
</style>
